package com.qidianit.mes.pro.controller;

import com.alibaba.excel.EasyExcel;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.pojo.pro.ProFeedback;
import com.qidianit.mes.pro.service.ProFeedbackService;
import com.qidianit.mes.vo.ProFeedbackPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Package Path: com.qidianit.mes.pro.controller
 * Author: Alhz
 * Description:
 * Date: 2025/9/25 16:29
 */
@RestController
@RequestMapping("/feedback")
public class ProFeedbackController {
    @Autowired
    private ProFeedbackService proFeedbackService;

    @GetMapping("/list")
    public Result selectFeedBackPage(ProFeedbackPageVO proFeedbackPageVO,
                                     @RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "10") int pageSize) {
        return  proFeedbackService.selectAll(proFeedbackPageVO,page,pageSize);
    }
    
    @GetMapping("/getById/{id}")
    public Result selectFeedBackById(@PathVariable Integer id){
        return proFeedbackService.selectFeedbackById(id);
    }

    @PostMapping("/add")
    public Result insertFeedBack(@RequestBody ProFeedback proFeedback){
        return proFeedbackService.insertFeedback(proFeedback);
    }

    @PostMapping("/update")
    public Result updateFeedBack(@RequestBody ProFeedback proFeedback){
        return proFeedbackService.updateFeedback(proFeedback);
    }

    @PostMapping("/delete/{id}")
    public Result deleteFeedBack(@PathVariable Integer id){
        return proFeedbackService.deleteFeedback(id);
    }

    @PostMapping("/batchDelete")
    public Result batchDeleteFeedBack(@RequestBody Long[] ids){
        return proFeedbackService.batchDeleteFeedback(ids);
    }

    @GetMapping("/export/feedback")
    public void exportFeedBack(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        response.setCharacterEncoding("UTF-8");

        String fileName = URLEncoder.encode("生产报工表","UTF-8").replaceAll("\\+", "%20");

        List<ProFeedback> proFeedbackList = proFeedbackService.selectAllFeedback();

        EasyExcel.write(response.getOutputStream(), ProFeedback.class)
                .sheet()
                .doWrite(proFeedbackList);
    }
}
